#!/bin/bash

# Tiny QA Docker 部署脚本

echo "========================================"
echo "Tiny QA - Docker 部署脚本"
echo "========================================"

# 检查Docker Compose文件
if [ ! -f "docker-compose.yml" ]; then
    echo "❌ 错误: docker-compose.yml 文件不存在"
    exit 1
fi

# 创建必要目录
echo "📁 创建必要目录..."
mkdir -p data/database data/documents data/vectors logs

# 设置权限
echo "🔐 设置目录权限..."
chmod 755 data logs
chmod 755 data/database data/documents data/vectors

echo ""
echo "🚀 启动Tiny QA服务..."

# 检查文档目录配置
echo "📁 检查文档目录配置..."
if ! grep -q "documents:/app/data/documents" docker-compose.yml; then
    echo "⚠️ 未配置文档目录，运行文档目录配置向导..."
    ./scripts/configure-documents.sh
fi

# 检查模型配置
echo "🤖 检查模型配置..."
if [ ! -f "models/model_config.txt" ]; then
    echo "⚠️ 未找到模型配置，运行模型设置向导..."
    ./scripts/setup-models.sh
fi

# 显示模型配置
if [ -f "models/model_config.txt" ]; then
    echo "📋 当前模型配置:"
    cat models/model_config.txt
    echo ""
fi

# 启动服务
echo "🐳 启动Docker服务..."
docker-compose up -d

if [ $? -eq 0 ]; then
    echo "✅ 服务启动成功!"
    
    echo ""
    echo "📊 服务状态:"
    docker-compose ps
    
    echo ""
    echo "🌐 访问地址:"
    echo "主页: http://localhost:5002"
    echo "API: http://localhost:5002/api"
    echo "Ollama: http://localhost:11434"
    
    echo ""
    echo "📋 常用命令:"
    echo "查看日志: docker-compose logs -f"
    echo "查看Tiny QA日志: docker-compose logs -f tiny-qa"
    echo "查看Ollama日志: docker-compose logs -f ollama"
    echo "停止服务: docker-compose down"
    echo "重启服务: docker-compose restart"
    echo "进入容器: docker exec -it tiny-qa bash"
    
    echo ""
    echo "🤖 模型管理:"
    echo "查看模型: docker exec -it tiny-qa-ollama ollama list"
    echo "下载新模型: docker exec -it tiny-qa-ollama ollama pull <model_name>"
    echo "重新配置模型: ./scripts/setup-models.sh"
    
else
    echo "❌ 服务启动失败"
    echo ""
    echo "🔍 查看错误日志:"
    docker-compose logs
    exit 1
fi 